package boerse.main.Logger;
import java.io.*;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.logging.*;

/**
 * Created with IntelliJ IDEA.
 * User: 13102012
 * Date: 09.05.13
 * Time: 16:41
 * To change this template use File | Settings | File Templates.
 */
public class GameLogger {
    private static Logger logger;
    private FileHandler fileHandler;
    private Formatter formatter;

    private GameLogger(String cfgGileName, ResourceBundle rb, Locale locale) throws IOException {
        FileReader fr = new FileReader(cfgGileName);
        BufferedReader br = new BufferedReader(fr);   
                   
        logger = Logger.getLogger("GameLogger");
        fileHandler = new FileHandler( rb.getString("fileName") + getDate(locale) + ".txt");
        formatter = new SimpleFormatter();
        DateFormat f = DateFormat.getDateInstance(DateFormat.MEDIUM, locale);
        fileHandler.setFormatter(formatter);
        logger.addHandler(fileHandler);

        setLoggerLevel(br.readLine());
        br.close();
    }

    private void setLoggerLevel (String loggerLevel) {
        switch (loggerLevel) {
            case ("FINEST") :
                logger.setLevel(Level.FINEST);
                break;
            case ("FINER") :
                logger.setLevel(Level.FINER);
                break;
            case ("FINE") :
                logger.setLevel(Level.FINE);
                break;
            case ("CONFIG") :
                logger.setLevel(Level.CONFIG);
                break;
            case ("INFO") :
                logger.setLevel(Level.INFO);
                break;
            case ("WARNING") :
                logger.setLevel(Level.WARNING);
                break;
            case ("SEVERE") :
                logger.setLevel(Level.SEVERE);
                break;
        }
    }

    private String getDate(Locale locale) {
        Calendar cal = Calendar.getInstance();
        Date date = cal.getTime();
        DateFormat dateFormatter = DateFormat.getDateInstance(DateFormat.FULL, locale);
        String newDate = dateFormatter.format(date);
        newDate = newDate.replace(" ", "__").replace( ".", "_").replace(":","_");
        return newDate;
    }

    public static Logger getLogger(String cfgName, ResourceBundle rb, Locale locale) throws IOException{
        new GameLogger(cfgName, rb, locale);
        return logger;
    }

    private String readIn() {
        return "";
    }

}
